<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Products\MixSku;

use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
class EnableItem extends BatchAction
{
    public $name = '启用';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            foreach ($collection as $model) {
                DB::beginTransaction();
                if($model->status == 1){
                    DB::rollBack();
                    continue;
                }
                $model->status = 1;
                $flag = $model->save();
                if(!$flag){
                    DB::rollBack();
                }else{
                    $count++;
                    DB::commit();
                }
            }
            return $this->response()->success("成功启用{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
